package com.market.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.market.dao.LocationDao;
import com.market.domain.Location;
import com.market.domain.PickupSchedule;
import com.market.domain.User;
import com.market.manager.LocationManager;

@Service("locationManager")
public class LocationManagerImpl extends
		GenericManagerImpl<LocationDao, Location, Long> implements
		LocationManager {

	@Autowired
	public void setDao(LocationDao locationDao) {
		this.dao = locationDao;
	}
	
	



	public List<User> fetchUsers(Location location) {
		return this.dao.findAllUsers(location);
	}





	public Location fetchOne(Long id) {
		Location location = this.dao.findById(id);
		location.setPickupSchedules(this.fetchPickupSchedules(location));
		return location;
	}

	public Location fetchOne(Location location) {
		if (location != null && location.getIsNew() == false) {
			return this.fetchOne(location.getId());
		} else {
			return location;
		}
	}

	public List<PickupSchedule> fetchPickupSchedules(Location location) {
		return this.dao.findPickupSchedules(location);
	}
	
	

	public List<Location> fetchAllOrderByNumber() {
		List<Location> locations = this.dao.findAllOrderByNumber();
		for(Location loc : locations){
			this.fetchOne(loc);
		}
		return locations;
	}

	@Override
	public List<Location> fetchAll() {
		// TODO Auto-generated method stub
		List<Location> locations = super.fetchAll();
		for(Location loc : locations){
			this.fetchOne(loc);
		}
		return locations;
	}

}
